package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.MessageDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.MessageMapper;
import com.iflytek.model.Message;
import com.iflytek.service.MessageService;
import com.iflytek.service.RoleService;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.MessageVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 留言表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;
    @Resource
    private MessageService messageService;

    @Resource
    private RoleService roleService;

    /**
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<List<MessageVO>> findAll() {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        List<Message> messageList = messageMapper.selectList(queryWrapper);
        List<MessageVO> messageVOList = CopyUtil.copyList(messageList, MessageVO.class);
        return Result.success(messageVOList);
    }

    /**
     * saveMessage
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveMessage(MessageDTO dto) {
        //Message message = new Message();
        if (dto == null) {
            return Result.error("1", "内容为空");
        }
        Message message = CopyUtil.copy(dto, Message.class);
        if (StringUtils.isEmpty(message.getId())) {
            if (StringUtils.isEmpty(message)) {
                return Result.error("1", "保存失败");
            }
//            message.setId(UuidUtil.getShortUuid());

        }
        messageMapper.insert(message);
        return Result.success(true);
    }

    /**
     * updateMessage
     * @param dto
     * @return
     */
    @Override
    public Result<Boolean> updateMessage(MessageDTO dto) {
        Message message = new Message();
        BeanUtils.copyProperties(dto, message);
        int count = messageMapper.updateById(message);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("1", "更新失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Result<MessageVO> findById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        Message message = messageMapper.selectById(id);
        MessageVO messageVO = CopyUtil.copy(message, MessageVO.class);

        return Result.success(messageVO);
    }


    /**
     * @param id
     * @return
     */
    @Override
    public Result<MessageVO> deleteById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        int i = messageMapper.deleteById(id);
        if (i > 0) {
            Result.success();
        } else {

            Result.error("204", "错误");
        }
        return Result.success();
    }


    /**
     * @param foreignId
     * @return
     */

    @Override
    public Result<List<MessageVO>> findByForeignId(Integer foreignId) {
        if (foreignId == null) {
            Result.error("401", "id为空");
        }
        List<Message> list = messageMapper.selectList(new QueryWrapper<Message>().eq("foreign_id", foreignId));
        List<MessageVO> messageVOS = CopyUtil.copyList(list, MessageVO.class);
        return Result.success(messageVOS);
    }


    /**
     * @param dto
     * @return
     */
    @Override
    public Result<PageVO<MessageVO>> findPage(PageDTO<MessageDTO> dto) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper();

        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        //分页
        PageHelper.startPage(dto.getPageNum(), dto.getSize());

        List<Message> messages = messageMapper.selectList(queryWrapper);

        List<MessageVO> messageVOList = new ArrayList<>();
        messageVOList = CopyUtil.copyList(messages, MessageVO.class);

        PageInfo<Message> messagePageInfo = new PageInfo<>(messages);
        PageVO<MessageVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(messagePageInfo.getTotal());
        pageVO.setRecords(messageVOList);
        return Result.success(pageVO);
    }

}

